var app = getApp()
var util = require('../../utils/util.js')
var md5 = require('../md5.js');
Page({
  data: {
    
  },
  submit: function () {
    console.log("aaaaaaaaaa")
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  topc: function () {
    wx.navigateTo({
      url: '../my/personalCenter',
    })
  },
  toBadge: function () {
    util.toBadge()
  },
  yc: function () {
    this.setData({
      showModal: false
    })
  },
  preventTouchMove: function () {

  },
  go: function () {
    this.setData({
      showModal: false
    })
  },
  onShow:function(){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Mine'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '我的'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/user/profile'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            myMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

      //获取头像
      var url1 = app.globalData.picoaddress + '/Attendee/user/getdeviceid'
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url1,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            grMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })
      //获取签到二维码
      var url2 = app.globalData.picoaddress + '/attendee/user/qrcode'
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url2,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          console.log('mesg=' + mesg)
          that.setData({
            ewmMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Mine'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '我的'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/user/profile'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            myMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

      //获取头像
      var url1 = app.globalData.picoaddress + '/Attendee/user/getdeviceid'
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url1,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          that.setData({
            grMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })
   //获取签到二维码
      var url2 = app.globalData.picoaddress + '/attendee/user/qrcode'
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url2,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          console.log('mesg=' + mesg)
          that.setData({
            ewmMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  toFeedback(e) {
    wx.navigateTo({
      url: '../feedback/feedback',
    })
  },
  ycewm(){
    this.setData({
      showModal: false
    })
  },
  toExpo: function () {
    wx.navigateTo({
      url: '../participateExhibition/participateExhibition',
    })
  },
  uploadPhoto(e) { // 拍摄或从相册选取上传
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log("base64=" + wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64"))
        var base64img = wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64") + ''
        //上传头像
        var exhibitionid = wx.getStorageSync('exhibitionid')
        var u = wx.getStorageSync('u')
        if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
          && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
          //默认最新（调用pico接口）
          var timestamp = Date.parse(new Date());
          timestamp = timestamp / 1000;
          var url = app.globalData.picoaddress + '/attendee/user/uploadphoto'
          var k = {}
          k["v"] = '1.0.0'
          k["client"] = 'ios'
          k["timestamp"] = timestamp
          k["lan"] = 'cn'
          k["exhibitionid"] = exhibitionid
          k["u"] = u
          k["base64img"] = base64img
          var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
          console.log(sign)
          var curb64 = md5.hexMD5(sign)
          console.log(curb64)
          k["sign"] = curb64
          wx.request({
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            url: url,
            method: 'post',
            data: k,
            success(res) {
              var exhibitionid = wx.getStorageSync('exhibitionid')
              var u = wx.getStorageSync('u')
              console.log('exhibitionid=' + exhibitionid)
              console.log('u=' + u)
              if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
                && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
                //默认最新（调用pico接口）
                var timestamp = Date.parse(new Date());
                timestamp = timestamp / 1000;
                var k = {}
                k["v"] = '1.0.0'
                k["client"] = 'ios'
                k["timestamp"] = timestamp
                k["lan"] = 'cn'
                k["exhibitionid"] = exhibitionid
                k["u"] = u
                var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
                console.log(sign)
                var curb64 = md5.hexMD5(sign)
                console.log(curb64)
                k["sign"] = curb64
                //重新获取头像
                var url = app.globalData.picoaddress + '/Attendee/user/getdeviceid'
                wx.request({
                  header: {
                    'content-type': 'application/x-www-form-urlencoded',
                  },
                  url: url,
                  method: 'post',
                  data: k,
                  success(res) {
                    console.log(res)
                    var mesg = res.data
                    console.log('mesg=' + mesg)
                    var curdata = mesg.data
                    that.setData({
                      grMesg: curdata
                    });
                  }, fail: res => {
                    console.log(res, "error")
                  }

                })
              }
            }, fail: res => {
              console.log(res, "error")
            }
          })
        }


      },
    })

  },
})